$(function() {
	// 获取当前页面的URL
	var fullUrl = window.location.href;

	// 提取查询字符串
	var queryString = fullUrl.split("?")[1];

	// 解析查询字符串
	var params = {};
	if (queryString) {
		var pairs = queryString.split('&');
		for (var i = 0; i < pairs.length; i++) {
			var pair = pairs[i].split('=');
			var key = decodeURIComponent(pair[0]);
			var value = decodeURIComponent(pair[1] || '');
			params[key] = value;
		}
	}

	var token = sessionStorage.getItem('token');

	var totalPrice = params.totalPrice;

	var checkStr = params.check_time;
	var leaveStr = params.leave_time;
	var rtPrice = params.rtPrice;
	var phone = params.phone;
	var hotelId = params.hotelId;

	var daysDiff = params.daysDiff;

	var checkStrs = checkStr.split('-');
	var leaveStrs = leaveStr.split('-');

	$(".time").html("入住日期：" + checkStrs[1] + "月" + checkStrs[2] + "日" + "-" + leaveStrs[1] + "月" + leaveStrs[
		2] + "日 共" + daysDiff + "晚");

	$(".price span").html(totalPrice);

	$(".pay-button").html("确认支付(还需要支付￥" + totalPrice + ")");

	var userBook = {
		phone: phone,
		checkTime: checkStr,
		leaveTime: leaveStr,
		bookPrice: rtPrice,
		hotelId: hotelId
	}


	$(".pay-button").click(function() {
		$.ajax({
			url: "http://localhost:8080/userBook/payment",
			method: "post",
			headers: {
				token: token
			},
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify(userBook),
			success: function(data) {
				if (data.t > 0) {
					alert('支付成功')
					window.location.href = 'detail.html?hotelId=' + hotelId;
				} else {
					alert('支付失败')
				}
			}
		})
	})


})